﻿/*
 *
 * @author
 * @version
 * Previous Vector2D class, rename to Point to compatible flash.geom.Point class in flash8
 */

class com.gamemeal.Point2 {
	public var x:Number;
	public var y:Number;
	
	public function Point2(ax:Number,ay:Number){
		x = ax;
		y = ay;
	}

	public function clone():Point2{
		var v = new Point2(x,y);
		return v;
	}

	static function distance(p1:Point2, p2:Point2) : Number{
		var dx = p2.x-p1.x;
		var dy = p2.y-p1.y;
		return Math.sqrt(dx*dx+dy*dy);
	}

	public function equals(p2:Point2):Boolean{
		return (x==p2.x&&y==p2.y);
	}

	static function interpolate(p1:Point2, p2:Point2, f:Number) : Point2{
		var dx = p1.x+p2.x;
		var dy = p1.y+p2.y;
		var v = new Point2(dx*f,dy*f);
		return v;
	}

	public function normalize(newMag:Number):Void{
		var scale = newMag/Math.sqrt((x*x)+(y*y));
		x *= scale;
		y *= scale;
	}


	public function offset(dx:Number, dy:Number):Void{
		x+=dx;
		y+=dy;
	}

	public function project(v2:Point2):Point2{
		var dp = dot(this,v2);
		var v = new Point2((dp/(v2.x*v2.x + v2.y*v2.y))* v2.x,(dp/(v2.x*v2.x+v2.y*v2.y))*v2.y);
		return v;
	}
 
	static function polar(l:Number, a:Number):Point2{
		var v = new Point2(l*Math.cos(a),l*Math.sin(a),0);
		return v;
	}
 
	public function toString():String{
		return "(x="+x+",y="+y+")";
	}

	public function subtract(v2:Point2) : Point2{
		var nv = new Point2(x-v2.x,y-v2.y);
		return nv;
	}

//original methods
	static function adds(v1,v2):Point2{
		var v=new Point2(v1.x+v2.x,v1.y+v2.y);
		return v;
	}

	public function norm():Void{
		var l = Math.sqrt((x*x)+(y*y));
		x /= l;
		y /= l;
	}

	public function scale(v:Point2,s:Number):Point2{
		var v = new Point2(v.x*s,v.y*s);
		return v;
	};
	
	static function dot(v1:Point2,v2:Point2):Number{
		return ((v1.x*v2.x)+(v1.y*v2.y));
	};

	static function dir(v:Point2):Point2{
		var l = Math.sqrt((v.x*v.x)+(v.y*v.y));
		var nv = new Point2(v.x/l,v.y/l);
		return nv;
	};

//properties
	function get angle(){
		return Math.atan2(y,x);
	}

	function set angle(newAngle:Number){//radian
		var m = Math.sqrt((x*x)+(y*y));
		x = m*Math.cos(newAngle);
		y = m*Math.sin(newAngle);
	}

	function get length() {
		return Math.sqrt((x*x)+(y*y));
	};	
	
	function set length(newMag:Number){
		var l = Math.sqrt((x*x)+(y*y));
		if(l!=0){
			var scale = newMag/l;
			x *= scale;
			y *= scale;
		}
	}
}